package org.example.demo5;

import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * @author Gong.Yang
 * netty的Future相比Java的Future，更加灵活，可以设置事件回调，无需阻塞主线程执行
 */
@Slf4j
public class Demo {
    public static void main(String[] args) {
        NioEventLoopGroup executors = new NioEventLoopGroup();
        Future<String> future = executors.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                log.info("【Callable】callable process ...");
                TimeUnit.SECONDS.sleep(3);
                return "hello";
            }
        });
//        String s = future.get(); 传统future 阻塞进程
        future.addListener(new GenericFutureListener<Future<? super String>>() {
            @Override
            public void operationComplete(Future<? super String> future) throws Exception {
                log.info("【Callable】process complete");
                log.info("【Callable】result-> {}", future.get());
            }
        });



        executors.shutdownGracefully();
        log.info("【Main】main not block");


    }
}
